﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Net.Mail;
using System.Net;

namespace Hired.employer
{
    public partial class create : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (User.IsInRole("employer"))
            {
                EmployerPassport ep = DBEmployerPassport.RetrieveEmployerPassport(User.Identity.Name);
                if ((bool)ep.Activated)
                {
                    Response.Redirect("home.aspx");
                }
            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (IsValid)
            {
                EmployerPassport ep = new EmployerPassport();
                CompanyAccount ca = new CompanyAccount();


                ep.FirstName = txtFirstName.Text;
                ep.LastName = txtLastName.Text;
                ep.Username = txtUsername.Text;
                ep.Password = Encryption.md5Encrypt(txtPassword.Text);
                ep.Email = txtEmail.Text;
                ep.SecurityQuestion = ddlSecurityQuestion.Text;
                ep.SecurityAnswer = txtSecurityAnswer.Text;
                ep.Administrator = cbAdministrator.Checked;
                ep.Activated = false;

                if (User.IsInRole("company"))
                {
                    ca = DBCompanyAccount.RetrieveCompanyAccount(User.Identity.Name, false);
                }
                else if (User.IsInRole("employer"))
                {
                    ca = DBCompanyAccount.RetrieveCompanyAccountByEmployerID(User.Identity.Name, false);
                }

                if (DBEmployerPassport.AddEmployerPassport(ep, ca))
                {
                    string subject = "Activate your Hired employer passport";
                    string body = "Dear Hired user,<br />" +
                        "A request was made to create an employer passport under your e-mail address.<br /><br />" +
                        "Your username: " + txtUsername.Text + "<br />" +
                        "Your password: " + txtPassword.Text + "<br /><br />" +
                        "You have to activate your passport before you can log in. You can go " +
                        "http://hired.com/activate.aspx?username=" + txtUsername.Text + "&activationID=" + Encryption.md5Encrypt(txtUsername.Text) +
                        " to activate your passport. Alternatively, you can go <a href=\"http://hired.com/activate.aspx\">here</a> and enter your activation ID.<br /><br />" +
                        "Your activation ID: " + Encryption.md5Encrypt(txtUsername.Text);
                    if (Email.Send(txtEmail.Text, subject, body))
                    {
                        Server.Transfer("../registersuccess.aspx");
                    }
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("../settings.aspx");
        }

        protected void CustomUsername_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (DBAccount.RetrieveAccountByUsername(txtUsername.Text) == null)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void CustomEmail_ServerValidate(object source, ServerValidateEventArgs args)
        {
            object account = DBAccount.RetrieveAccountByEmail(txtEmail.Text);
            string accountType = DBAccount.RetrieveAccountTypeByAccount(account);
            if (DBAccount.RetrieveAccountByEmail(txtEmail.Text) == null)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}